package cn.edu.neusoft.yuejian4.yuejian_4.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import cn.edu.neusoft.yuejian4.yuejian_4.R;
import cn.edu.neusoft.yuejian4.yuejian_4.adapter.MyLikeClubAdapter;
import cn.edu.neusoft.yuejian4.yuejian_4.javaBean.Club;
import cn.edu.neusoft.yuejian4.yuejian_4.javaBean.Success;
import cn.edu.neusoft.yuejian4.yuejian_4.listener.List_TListener;
import cn.edu.neusoft.yuejian4.yuejian_4.listener.TListener;
import cn.edu.neusoft.yuejian4.yuejian_4.model.ClubModel;
import cn.edu.neusoft.yuejian4.yuejian_4.model.MyModel;

/**
 * Created by Lix on 2018/10/12.
 */

public class MyLikeClubActivity extends AppCompatActivity {

    private ImageView back;
    private RecyclerView recyclerView;
    private SharedPreferences sp;
    private List testlist;
    private int uid;
    private SwipeRefreshLayout swipeRefreshLayout;
    private  MyLikeClubAdapter myLikeClubAdapter;
    private Button btn_unlike;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_like_club);
        sp = getSharedPreferences("userinfo",MODE_PRIVATE);
        uid = sp.getInt("uid",4);
        initView();
        initEvent();
        MyModel myModel = new MyModel();
        myModel.mylikeclub(uid,likeclublistener);

    }

    List_TListener<Club> likeclublistener = new List_TListener<Club>() {
        @Override
        public void onResponse(final List<Club> list) {
                myLikeClubAdapter = new MyLikeClubAdapter(getApplicationContext(),list);
                recyclerView.setAdapter(myLikeClubAdapter);
                myLikeClubAdapter.setmOnItemClickListener(new MyLikeClubAdapter.mOnItemClickListener() {
                    @Override
                    public void onClick(View view, final int position) {
                        switch (view.getId()){
                            case R.id.unlike_club_btn:
                                ClubModel clubModel = new ClubModel();
                                clubModel.unlikeclub(list.get(position).getId(), uid, new TListener<Success>() {
                                    @Override
                                    public void onResponse(Success success) {
                                        if(success.getError().equals("0")){
                                            Toast.makeText(MyLikeClubActivity.this,"取消关注成功！", Toast.LENGTH_SHORT).show();
                                            refresh();
                                        }

                                    }

                                    @Override
                                    public void onFail(String msg) {

                                    }
                                });
                                break;
                        }
                    }
                });

        }

        @Override
        public void onFail(String msg) {

        }
    };

    public void refresh() {
        Intent intent = new Intent(MyLikeClubActivity.this, MyLikeClubActivity.class);
        startActivity(intent);
        finish();
    }


    private void initView(){
        recyclerView = (RecyclerView)findViewById(R.id.rv_like_club);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this.getApplicationContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        back = (ImageView) findViewById(R.id.my_like_club_back);
        swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swiper_my_like_club);
        swipeRefreshLayout.setColorSchemeColors(Color.BLUE);


    }
    private void initEvent() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyModel myModel = new MyModel();
                switch (v.getId()) {
                    case R.id.my_like_club_back:
                        finish();
                        break;

                }
            }
        });
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                MyModel myModel = new MyModel();
                myModel.mylikeclub(uid, likeclublistener);
                swipeRefreshLayout.setRefreshing(false);
            }
        });

    }
}

